<?php
/*
 * @param auther: feyeen Date: 2016/4/16 Time: 11:16
 * @param SelectWidget.php
 */

namespace common\widgets;

use Yii;

class SelectWidget extends \yii\bootstrap\Widget
{
    public $SelectData = '';
    public $value = '';
    public $id = '';
    public $isOr = '1';

    public function run(){
        //初始化数据
        parent::init();

        //组装option
        $optionsBox = $this->options($this->SelectData,$this->value);

        //组装select
        if($this->isOr ==1)
            $selectDrop = $this->select($optionsBox,$this->id);
        else
            $selectDrop =$optionsBox;
        //返回数据
        return $selectDrop;
    }

    //options str
    private function options($SelectData,$value){
        //加载运费配置文件

        $optionStr = '';
        $optionStr.="<option value=''>请选择</option>";
        foreach($SelectData as $v){

            $optionStr .= "<option value='{$v->uid}' ".($value==$v->uid?'selected':'').">{$v->name}</option>";

        }
       return $optionStr;

    }
    //select str
    private function select($optionsBox,$id){
        $selectDrop = '';
        $selectDrop .= "<select id='{$id}' name='{$id}' >";
        $selectDrop .= $optionsBox;
        $selectDrop .= "</select>";
        return $selectDrop;
    }
}
